package com.houserent.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.houserent.dao.LogDao;
import com.houserent.entry.LogInfo;

public class HouseUtils {

    /**
     * 房间图片的根路径
     */
    public static final String PIC_PATH = "D:/Java/Tomcat/apache-tomcat-7.0.65/webapps/HouseRent/housePic/";

    public static boolean isEmpty(List list) {
        return null == list || 0 == list.size();
    }

    public static boolean isEmpty(Map map) {
        return null == map || map.isEmpty();
    }

    public static boolean isEmpty(Object o) {
        return null == o || "".equals(o);
    }

    public static boolean isEmpty(String[] strs) {
        return null == strs || 0 == strs.length;
    }

    /**
     * 判断传入的多个参数是否有空的,有则返回true,都不为空返回false
     * @param object
     * @return
     */
    public static boolean isEmpty(Object... object) {
        for (Object o : object) {
            if (isEmpty(o)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getSysDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    /**
     * 获取当前时间
     * @return
     */
    private static String getSysDate(String parent) {
        SimpleDateFormat format = new SimpleDateFormat(parent);
        return format.format(new Date());
    }

    /**
     * 转换时间格式
     * @param time
     * @param pattern
     * @return
     */
    public static String TransDate(String time, String pattern, String toPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(time);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        sdf = new SimpleDateFormat(toPattern);
        return sdf.format(date);
    }

    /**
     * 生成Map
     * @param objects
     * @return
     */
    public static Map asMap(Object... objects) {
        Map m = new HashMap(objects.length / 2 + 1);
        int i = 0;
        for (; i < objects.length - 1; i += 2) {
            m.put(objects[i], objects[i + 1]);
        }

        if (i < objects.length) {
            m.put(objects[i], null);
        }
        return m;
    }

    /**
     * 生成房间ID,支持批量生成
     * @return
     */
    public static String createHouseID() {
        try {
            Thread.sleep(1);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "house" + getSysDate("yyMMddHHmmssSSS");
    }

    /**
     * 生成订单ID
     * @return
     */
    public static String createOrderID() {
        return createID("order");
    }

    /**
     * 生成消息ID
     * @return
     */
    public static String createMsgID() {
        try {
            Thread.sleep(1);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "msg" + getSysDate("yyMMddHHmmssSSS");
    }

    /**
     * 生成费用ID
     * @return
     */
    public static String createAcctID() {
        return createID("acct");
    }

    /**
     * 生成用户临时ID
     * @return
     */
    public static String createUserID(LogDao logDao, String houseNumber) {
        LogInfo userIdInfo = logDao.login("createUserId");
        String userId = userIdInfo.getCert_num();// 用户个数
        int count = Integer.valueOf(userId) + 1;
        if (count < 10) {
            userId = "000" + count;
        }
        else if (count < 100) {
            userId = "00" + count;
        }
        else {
            userId = "0" + count;
        }
        userIdInfo.setCert_num(userId);
        logDao.fullUserInfo(userIdInfo);
        return houseNumber + userId;
    }

    /**
     * 生成用户临时密码
     * @return
     */
    public static String createUserPassWord() {
        // String date = getSysDate();
        // return date.substring(1, 3) + date.substring(5, 7) + date.substring(9, 11);
        return "123456";
    }

    /**
     * 自动生成ID
     * @return
     */
    private static String createID(String flag) {
        return flag + getSysDate();
    }

    /**
     * 比较第一个时间参数是否比第二个时间参数偏后
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isBig(String time1, String time2) {
        return time1.compareTo(time2) > 0;
    }

    /**
     * 转换日期格式
     * @param datetime
     * @param pattern
     * @param field 偏移类型
     * @param amount 偏移量
     * @return
     */
    public static String rollDateStr(String datetime, String pattern, int field, int amount) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(datetime);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.add(field, amount);
        date = cd.getTime();
        return sdf.format(date);
    }

    /**
     * @param fDate1
     * @param oDate1
     * @param pattern
     * @return
     */
    public static int daysOfTwo(String fDate1, String oDate1, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date fDate = null;
        Date oDate = null;
        try {
            fDate = sdf.parse(fDate1);
            oDate = sdf.parse(oDate1);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        long time1 = fDate.getTime();
        long time2 = oDate.getTime();
        return (int) ((time1 - time2) / (1000 * 60 * 60 * 24));
    }

    /**
     * 获取日期的月份
     * @param dateTime
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static int getDateMonth(String dateTime, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = sdf.parse(dateTime);
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        return aCalendar.get(Calendar.MONTH);
    }

}
